package dao

import "chatting/model"

func AddUser(user *model.User) (info *model.User, err error) {
	gdb.Create(&user)
	return user, nil
}

func GetUserFromName(username string) *model.User {
	user := new(model.User)
	gdb.Preload("Friends").Where("username = ?", username).First(user)
	return user
}

func AddFriend(user1 *model.User, user2 *model.User) error {
	user1.Friends = append(user1.Friends, user2)
	user2.Friends = append(user2.Friends, user1)
	err := gdb.Updates(user2).Error
	if err != nil {
		return err
	}
	return gdb.Preload("Friends").Updates(user1).Error
}
